import streamlit as st
from model_utils import ModelLoader

# 初始化模型加载器
model_loader = ModelLoader()

# Streamlit 应用
st.title("Image to Text Generator")
st.write("Upload an image and generate a text description based on the image.")

uploaded_file = st.file_uploader("Choose an image...", type=["jpg", "png", "jpeg"])

if uploaded_file is not None:
    # 保存上传的文件到临时目录
    with open("temp_image.png", "wb") as f:
        f.write(uploaded_file.getbuffer())
    
    # 显示上传的图像
    st.image("temp_image.png", caption="Uploaded Image", use_column_width=True)
    
    # 生成文本
    with st.spinner("Generating text..."):
        generated_text = model_loader.generate_text_from_image("temp_image.png")
    
    # 显示生成的文本
    st.success("Text generated successfully!")
    st.write("Generated Text:")
    st.write(generated_text)


# 下次一定要请教别人学会代码的冗余操作的处理写法，优化代码结构，现在只会在上一层进行调用，不然就寄了
# 虽然也明白这是优化代码，尽量不要让包之间出现交互关系，高内聚，低耦合的办法，但是有些确实无法避免